import { combineReducers, configureStore } from '@reduxjs/toolkit';
import { persistReducer, persistStore } from 'redux-persist'
import storage from 'redux-persist/lib/storage'

import global from './modules/global'
import menu from './modules/menu'
import breadcrumb from './modules/breadcrumb'
import tabs from './modules/tabs'
import auth from './modules/auth'

// 创建reducer(合并拆分的reducer)
const rootReducers = combineReducers({
  global,
  menu,
  breadcrumb,
  tabs,
  auth,
})

// 持久化配置
const persistConfig = {
  key: "reduxState",
  storage,
    // whitelist: ["global"], // 需要持久化保存的模块，默认保存所有模块
}

// 创建持久化后的reducer
const persistedReducer = persistReducer(persistConfig, rootReducers)

const store = configureStore({
  reducer: persistedReducer,
  middleware: (getDefaultMiddleware) => getDefaultMiddleware({
    serializableCheck: false,
  })
})

// 创建持久化后的store
const persistedStore = persistStore(store)

export type RootState = ReturnType<typeof store.getState>
export { store, persistedStore }